package com.bfly.mall.product.api;

import com.bfly.common.ResponseUtil;
import com.bfly.common.page.Pager;
import com.bfly.common.util.DataConvertUtils;
import com.bfly.core.base.controller.BaseController;
import com.bfly.core.base.entity.ResponseData;
import com.bfly.core.context.PagerThreadLocal;
import com.bfly.mall.product.dto.*;
import com.bfly.mall.product.entity.*;
import com.bfly.mall.product.service.IProductService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商品API
 *
 * @author 长沙商脉科技有限公司
 * @date 2020/9/21 19:42
 */
@RestController
@RequestMapping(value = "/api/product")
public class ProductApiController extends BaseController {

    @Autowired
    private IProductService productService;

    /**
     * 产品列表
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/9/23 16:22
     */
    @GetMapping(value = "/list")
    public void list(HttpServletResponse response, HttpServletRequest request) {
        PagerThreadLocal.set(request);
        String tag = request.getParameter("tag");
        String categoryId=request.getParameter("categoryId");
        String name=request.getParameter("name");

        Map<String, Object> exact = new HashMap<>(1);
        Map<String,String> unExact=new HashMap<>(1);
        exact.put("is_on_shelf", true);

        if (StringUtils.isNotBlank(tag)) {
            exact.put("tag", DataConvertUtils.convertToInteger(tag));
        }
        if (StringUtils.isNotBlank(categoryId)) {
            exact.put("category_id", DataConvertUtils.convertToInteger(categoryId));
        }
        if (StringUtils.isNotBlank(name)) {
            unExact.put("name", name);
        }

        Map<String,Boolean> sortMap=new HashMap<>(2);
        sortMap.put("seq",true);
        sortMap.put("id",false);

        Pager<Product> pager = productService.getPage(exact,unExact,sortMap);
        Pager<ProductListDTO> dtoPager = new Pager<>(pager.getPageNo(), pager.getPageSize(), pager.getTotalCount());

        if (pager.getData() != null) {
            List<ProductListDTO> list = new ArrayList<>();
            for (Product product : pager.getData()) {
                ProductListDTO dto = new ProductListDTO();
                BeanUtils.copyProperties(product, dto);

                dto.setCategoryName(product.getCategory()==null?"":product.getCategory().getName());

                String templateName = product.getFreightTemplate() == null ? "" : product.getFreightTemplate().getName();
                dto.setFreightTemplateName(templateName);
                dto.setSales(product.getVirtualSales());
                list.add(dto);
            }
            dtoPager.setData(list);
        }
        ResponseUtil.writeJson(response, ResponseData.getSuccess(dtoPager));
    }

    /**
     * 产品详情
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/9/24 12:11
     */
    @GetMapping(value = "/detail")
    public void detailProduct(HttpServletResponse response, @RequestParam(value = "productId", defaultValue = "0") int productId) {
        Product product = productService.get(productId);
        ProductFormDTO formDTO = new ProductFormDTO();
        BeanUtils.copyProperties(product, formDTO);

        if (product.getTxt() != null) {
            ProductTxtFormDTO txtFormDTO = new ProductTxtFormDTO();
            BeanUtils.copyProperties(product.getTxt(), txtFormDTO);
            formDTO.setTxt(txtFormDTO);
        }
        if (product.getImgs() != null) {
            List<ProductImgFormDTO> imgs = new ArrayList<>();
            product.getImgs().forEach(productImg -> {
                ProductImgFormDTO imgFormDTO = new ProductImgFormDTO();
                BeanUtils.copyProperties(productImg, imgFormDTO);
                imgs.add(imgFormDTO);
            });
            formDTO.setImgs(imgs);
        }
        if (product.getAttrs() != null) {
            List<ProductAttrFormDTO> attrs = new ArrayList<>();
            product.getAttrs().forEach(productAttr -> {
                ProductAttrFormDTO attrFormDTO = new ProductAttrFormDTO();
                BeanUtils.copyProperties(productAttr, attrFormDTO);
                attrs.add(attrFormDTO);
            });
            formDTO.setAttrs(attrs);
        }
        if (product.getSpeAttrs() != null) {
            List<ProductSpeAttrFormDTO> speAttrs = new ArrayList<>();
            product.getSpeAttrs().forEach(speAttr -> {
                ProductSpeAttrFormDTO speAttrFormDTO = new ProductSpeAttrFormDTO();
                BeanUtils.copyProperties(speAttr, speAttrFormDTO);
                speAttrFormDTO.setData(speAttr.getItems());
                speAttrs.add(speAttrFormDTO);
            });
            formDTO.setSpeAttrs(speAttrs);
        }
        if (product.getSkus() != null) {
            List<ProductSkuFormDTO> skus = new ArrayList<>();
            product.getSkus().forEach(sku -> {
                ProductSkuFormDTO skuFormDTO = new ProductSkuFormDTO();
                BeanUtils.copyProperties(sku, skuFormDTO);
                skus.add(skuFormDTO);
            });
            formDTO.setSkus(skus);
        }
        ResponseUtil.writeJson(response, ResponseData.getSuccess(formDTO));
    }
}